package module.map.newMap
{
	import drongo.desktop.utils.StringUtil;
	import drongo.desktop.utils.UtilsFile;
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.propertys.NumberProperty;
	import drongo.model.propertys.StringProperty;
	
	import flash.events.Event;
	import flash.filesystem.File;
	
	import common.view.FGUIMediator;
	import common.window.AlertDialog;
	
	import module.map.model.MapModel;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	
	import ui.Map.UI_CreateMapWindow;
	
	public class NewMapWindowMediator extends FGUIMediator
	{
		[Inject]
		public var view:NewMapWindow;
		[Inject]
		public var project:ProjectModel;
		
		public function NewMapWindowMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.AddEvents();
		}
		override public function destroy():void{
			super.destroy();
			this.view=null;
		}
		
		private function AddEvents():void
		{
			this.viewCom.m_closeButton.addClickListener(this.CloseButtonClick);
			this.viewCom.m_createBtn.addClickListener(this.CreateButtonClick);
		}
		
		private function RemoveEvents():void{
			this.viewCom.m_closeButton.removeClickListener(this.CloseButtonClick);
			this.viewCom.m_createBtn.removeClickListener(this.CreateButtonClick);
		}
		
		private function CloseButtonClick(e:Event=null):void{
			this.view.hideImmediately();
			this.view.dispose();
			this.destroy();
		}
		
		private function CreateButtonClick(e:Event):void{
			if(StringUtil.IsEmpty(this.viewCom.m_nameInput.text)){
				AlertDialog.open(this.view.root,"地图名称不能为空！");
			}
			if(StringUtil.IsEmpty(this.viewCom.m_idInput.text)){
				AlertDialog.open(this.view.root,"地图ID不能为空!");
			}
			if(StringUtil.IsEmpty(this.viewCom.m_mapWidth.text)){
				AlertDialog.open(this.view.root,"地图宽度不能为空!");
			}
			if(StringUtil.IsEmpty(this.viewCom.m_mapHeight.text)){
				AlertDialog.open(this.view.root,"地图高度不能为空!");
			}
			if(StringUtil.IsEmpty(this.viewCom.m_navWidth.text)){
				AlertDialog.open(this.view.root,"地图导航空间宽度不能为空!");
			}
			if(StringUtil.IsEmpty(this.viewCom.m_navHeight.text)){
				AlertDialog.open(this.view.root,"地图导航空间高度不能为空!");
			}
			var mapData:MapModel=new MapModel();
			//ID
			mapData.Add(new StringProperty(Propertys.ID,this.viewCom.m_idInput.text));
			//name
			mapData.Add(new StringProperty(Propertys.NAME,this.viewCom.m_nameInput.text));
			//测试状态
			mapData.Add(new NumberProperty(Propertys.TEST,0));
			//宽高
			mapData.Add(new NumberProperty(Propertys.WIDTH,Number(this.viewCom.m_mapWidth.text)));
			mapData.Add(new NumberProperty(Propertys.HEIGTH,Number(this.viewCom.m_mapHeight.text)));
			//是否需要保存
			mapData.Add(new NumberProperty(Propertys.STATE));
			//导航数据 w,h
			mapData.Add(new NumberProperty(Propertys.NAV_WIDTH,Number(this.viewCom.m_navWidth.text)))
			mapData.Add(new NumberProperty(Propertys.NAV_HEIGTH,Number(this.viewCom.m_navHeight.text)));
			mapData.Add(new ArrayProperty(Propertys.NODES));
			
			var mapList:ArrayProperty=this.project.project.Get(Propertys.MAPS) as ArrayProperty;
			if(mapList.containProperty(new StringProperty(Propertys.ID,this.viewCom.m_idInput.text))){
				AlertDialog.open(this.view.root,"地图已存在！");
			}
			mapList.Push(mapData);
			
			var mapID:String=this.viewCom.m_idInput.text;
			//保存地图配置
			var configFile:File=new File(project.mapsFile.nativePath+File.separator+mapID+".json");
			var jsonData:Object=mapData.Encode(0);
			UtilsFile.saveJson(configFile,JSON.stringify(jsonData,null," "));
			
			this.CloseButtonClick();
		}
		
		private function get viewCom():UI_CreateMapWindow{
			return this.view.view;
		}
	}
}